package com.example.xunua_zhinengjiaotong.Dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.xunua_zhinengjiaotong.R;
import com.example.xunua_zhinengjiaotong.utils.HttpUtil;
import com.example.xunua_zhinengjiaotong.utils.MyApp;

import org.json.JSONObject;


public class CarSpeedDialog3 extends AlertDialog{
    private Context context;
    private Spinner spinner;
    private Button btn_chaxun,btn_chongzhi,btn_submit;
    private LinearLayout line2,line3;
    private MyApp myApp;
    private EditText et;
    private TextView tv,tv_money;
    private HttpUtil util=new HttpUtil();


    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
//                    更新余额
                    tv_money.setText(msg.obj+"元");
                    break;
                case 1:
                    Toast.makeText(context, "充值成功！", Toast.LENGTH_SHORT).show();
                    int i= (int) (spinner.getSelectedItemId()+1);
                    getmoney(i+"");
                    break;
            }
        }
    };


    public CarSpeedDialog3(Context context,MyApp myApp) {
        super(context,R.style.myDialog);
        this.context=context;
        this.myApp=myApp;
    }

    protected CarSpeedDialog3(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    protected CarSpeedDialog3(Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_dialog_car_speed3);
        initView();
    }

    private void initView() {
       getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
       et=findViewById(R.id.car3_et);
       tv=findViewById(R.id.car3_tv_money);
       tv_money=findViewById(R.id.car3_tv_money);
       spinner = findViewById(R.id.trolley_dialog3_spinner);
       spinner.setAdapter(new ArrayAdapter<>(context, R.layout.support_simple_spinner_dropdown_item,
               new String[]{"一号小车", "二号小车", "三号小车", "四号小车"}));
       btn_chaxun=findViewById(R.id.trolley_dialog3_btn_chaxun);
       line2=findViewById(R.id.car3_line02);
       line3=findViewById(R.id.car3_line03);
       btn_chaxun.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
                line2.setVisibility(View.VISIBLE);
               int id= (int) (spinner.getSelectedItemId()+1);
               getmoney(id+"");
           }
       });
       btn_chongzhi=findViewById(R.id.trolley_dialog3_btn_chongzhi);
       btn_submit=findViewById(R.id.trolley_dialog3_btn_submit);
       btn_chongzhi.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if (line3.getVisibility() == View.GONE) {
                   line3.setVisibility(View.VISIBLE);
               }else {
                   line3.setVisibility(View.GONE);
               }
           }
       });

       btn_submit.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if (et.getText().toString().isEmpty()|et.getText().toString().equals("0")){
                   Toast.makeText(context, "请输入正确的充值金额", Toast.LENGTH_SHORT).show();
                   return;
               }
               int id= (int) (spinner.getSelectedItemId()+1);
               addMoney(id+"",et.getText().toString().trim());
           }
       });
    }
    private void getmoney(final String id){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject getCarAccountBalance = util.connectServer("{\"CarId\":" + id + "}", myApp, "GetCarAccountBalance");
                    String balance = getCarAccountBalance.getString("Balance");
                    Message message = new Message();
                    message.what=0;
                    message.obj=balance;
                    handler.sendMessage(message);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void addMoney(final String id, final String addmoney){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject setCarAccountRecharge = util.connectServer("{\"CarId\":"+id+",\"Money\":"+addmoney+"}", myApp, "SetCarAccountRecharge");
                    String result = setCarAccountRecharge.getString("result");
                    if (result.equals("ok")) {
                        Message message = new Message();
                        handler.sendEmptyMessage(1);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
